$(function(){

var nr_of_pics = $("#myImages > div").length;

$(".uploadmorepics").click(function(){
	if($("#myImages > div").length >= 15)
	{
		$(this).remove();
		
	}

});


	$('#submitImg').click(function(event){
	
		var check = true;
		var input_arr = [];
		
		input_arr = $('.uploadInput');
		
		
		
		
		
		input_arr.each(function(index,item){
			
			if (!item.value.replace(/^.+\.((jpg)|(png)|(jpeg))$/,"")==""){
				 check = false;
			}			
		});
			if (check == false){
				alert('Alla bilder har inte rätt filformat');
				event.preventDefault();
			}
	});
	

	$('.draggable').draggable({ 
	
		drag: function(event, ui){
		console.debug(event);
						
			$(this).css({
				position: "relative" 
			}); 
		},
		
	//	cursorAt: { cursor: "move", top: 37, left: 37 },
		
		revert: "invalid" /* function(event, ui){
		
			return $(this).css({
				position: "static" 
			});   */
		
	});
	
	$( ".draggable" ).bind( "dragrevert", function(event, ui){
  			
  			$(this).css({
				position: "static" 
			});
		});
	
	
	$('.dropzone1').droppable({ 
	
		drop: function(event, ui){
		
			ui.draggable.appendTo(this);

			ui.draggable.css({
				position: "static" 
			});
		},
		
		accept: ".draggable"

	});
	
	$('.dropzone2').droppable({ 
	
		drop: function(event, ui){
			
			ui.draggable.appendTo(this);

			ui.draggable.css({
				position: "static" 
			});
		},
		
		accept: ".default"

	});
	
	$('.dropzone3').droppable({ 
	
		drop: function(event, ui){
			
			ui.draggable.appendTo(this);

			ui.draggable.css({
				position: "static" 
			});
		},
		
		accept: ".myimg"

	});
	


	

	$(".deleteArea").droppable({
	 
	 	accept: '.draggable',
	 	hoverClass: 'dropAreaHover',
	 	drop: function(event, ui) {
	 	
	 		deleteImage(ui.draggable,ui.helper);
	 	},
	 
	 	activeClass: 'dropAreaHover'
	
	});
	 
	 function deleteImage($draggable,$helper){

	 	$.ajax({ url: 'deleteImg.php', type: 'GET', data: 'ajax=1&deleteArea=' + ($draggable).attr('src'), 
	 	 		
 		});

	 	
	 
	 	$helper.effect('transfer', { to: '.droppable', className: 'ui-effects-transfer' },500);
	 	$draggable.remove();
	 
	 }  

});


